﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SMITextEditor
{
    public partial class TextEditor : Form
    {
        private List<string> keys = new List<string>();
        private Dictionary<string, string> texts = new Dictionary<string, string>();

        /// <summary>
        /// 
        /// </summary>
        public TextEditor()
        {
            InitializeComponent();
            if(Properties.Settings.Default.lastFile != "")
            {
                openFile(Properties.Settings.Default.lastFile);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void onBtnOpenClick(object sender, EventArgs e)
        {
            using(OpenFileDialog dlg = new OpenFileDialog())
            {
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    Properties.Settings.Default.lastFile = dlg.FileName;
                    Properties.Settings.Default.Save();
                    openFile(dlg.FileName);
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="filePath"></param>
        private void openFile(string filePath)
        {
            using(System.IO.StreamReader reader = new System.IO.StreamReader(filePath))
            {
                string line;
                line = reader.ReadLine();
                while(line != null)
                {
                    parseString(line);
                    line = reader.ReadLine();
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        private void parseString(string str)
        {
            if (str == "")
                return;
            if (str[0] == '[')
                return;

            string[] subStrings = str.Split("=".ToCharArray(), 2);

            string key = subStrings[0];
            key = key.Replace(" ", "");


            string value = subStrings[1];
            int firstQuote = value.IndexOf('"');
            int lastQuote = value.LastIndexOf('"');
            value = value.Substring(firstQuote + 1, lastQuote - firstQuote - 1);

            keys.Add(key);
            texts.Add(key, value);
        }
    }
}
